
package com.idealighter.game.games.xzdd.util;

import com.idealighter.game.core.tuple.Pair;
import com.idealighter.game.dictionary.dic.XzddMultipleDic;
import com.idealighter.game.dictionary.dic.XzddRoomDic;
import com.idealighter.game.games.xzdd.dto.HuPaiItemInfo;
import com.idealighter.game.games.xzdd.struct.XzddHuPaiType;
import com.idealighter.game.games.xzdd.struct.XzddHuType;
import com.idealighter.game.games.xzdd.struct.XzddSeatCards;
import com.idealighter.game.server.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

/**
 * 血战到底计算器，计算翻数 .
 * 
 * @date 2016年9月21日 下午4:18:04
 *
 */
public class XzddCalculator {
  // 清一色加翻id
  public static final int QING_YI_SE_MULTIPLE_ID = 10;
  // 四合加翻id
  public static final int SI_HE_MULTIPLE_ID = 11;
  // 查花猪加翻id
  public static final int CHA_HUA_ZHU_MULTIPLE_ID = 32;
  // 退出游戏赔偿翻数
  public static final int EXIT_GAME_MULTIPLE = 16;

  private XzddCalculator() {}

  /**
   * 计算胡牌加翻明细数据(胡的类型、胡牌的牌型、清一色、四合) .
   * 
   * @param cards .
   * @param huType . 胡的类型
   * @param huPaiType . 胡的牌的类型
   * @return totalMultiple,items
   */
  public static Pair<Integer, List<HuPaiItemInfo>> calcHuData(XzddSeatCards cards,
      XzddHuType huType, XzddHuPaiType huPaiType) {
    final XzddMultipleDic multipleDic = ApplicationContext.getBean(XzddMultipleDic.class);
    // 基础金币
    int baseGold = ApplicationContext.getBean(XzddRoomDic.class)
        .get(cards.seat.table.room.getId().intValue()).getMinOne();
    final List<HuPaiItemInfo> items = new ArrayList<>();


    /*
     * 胡的类型计算(1:平胡,2:天胡,3:地胡,4:抢杠胡,5:杠上花,6:杠上炮) .
     */
    HuPaiItemInfo huItem = new HuPaiItemInfo();
    // 胡的倍数
    int huMultiple = huType.multiple();
    int huGold = baseGold << (huMultiple - 1);
    huItem.setType(huType.multipleId());
    huItem.setMultiple(huMultiple);
    huItem.setGolds(huGold);
    items.add(huItem);

    // 合计胡的翻数
    int totalMultiple = 0;

    totalMultiple += huMultiple;

    /*
     * 胡牌的牌型计算(7:小七对,8:大对,9:龙对) .
     */
    if (huPaiType != null && huPaiType != XzddHuPaiType.GENERAL) {
      HuPaiItemInfo huPaiTypeItem = new HuPaiItemInfo();
      int huPaiMultiple = huPaiType.multiple();
      int huPaiGold = huPaiMultiple * baseGold;
      huPaiTypeItem.setType(huPaiType.multipleId());
      huPaiTypeItem.setMultiple(huPaiMultiple);
      huPaiTypeItem.setGolds(huPaiGold);
      items.add(huPaiTypeItem);

      totalMultiple += huPaiMultiple;
    }

    /*
     * 清一色计算(10:清一色) .
     */
    int qingYiSeMultiple = multipleDic.get(QING_YI_SE_MULTIPLE_ID).getMultiple();
    int qingYiSeGold = baseGold << (qingYiSeMultiple - 1);
    if (cards.isQingYiSe()) {
      HuPaiItemInfo qingYiSeItem = new HuPaiItemInfo();
      qingYiSeItem.setType(QING_YI_SE_MULTIPLE_ID);
      qingYiSeItem.setMultiple(qingYiSeMultiple);
      qingYiSeItem.setGolds(qingYiSeGold);
      items.add(qingYiSeItem);

      totalMultiple += qingYiSeMultiple;
    }

    /*
     * 四合计算(11:四合) .
     */
    int siHeMultiple = multipleDic.get(SI_HE_MULTIPLE_ID).getMultiple();
    int siHeGold = baseGold << (siHeMultiple - 1);
    int siHeCount = cards.siHeCount(huType, huPaiType);

    for (int i = 0; i < siHeCount; i++) {
      HuPaiItemInfo siHeItem = new HuPaiItemInfo();
      siHeItem.setType(SI_HE_MULTIPLE_ID);

      siHeItem.setMultiple(siHeMultiple);
      siHeItem.setGolds(siHeGold);
      items.add(siHeItem);

      totalMultiple += siHeMultiple;
    }

    return new Pair<>(totalMultiple, items);
  }

  /**
   * 计算胡的倍数,牌满足3n+2 .
   * 
   * @param cards .
   * @param huType .
   * @param huPaiType . 胡牌类型
   * @return
   */
  public static int calcHuMultiple(XzddSeatCards cards, XzddHuType huType,
      XzddHuPaiType huPaiType) {
    XzddMultipleDic multipleDic = ApplicationContext.getBean(XzddMultipleDic.class);
    // 合计胡的翻数
    int totalMultiple = 0;

    /*
     * 胡的类型计算(1:平胡,2:天胡,3:地胡,4:抢杠胡,5:杠上花,6:杠上炮) .
     */
    totalMultiple += huType.multiple();

    /*
     * 胡牌的牌型计算(7:小七对,8:大对,9:龙对) .
     */
    if (huPaiType != null && huPaiType != XzddHuPaiType.GENERAL) {
      totalMultiple += huPaiType.multiple();
    }

    /*
     * 清一色计算(10:清一色) .
     */
    int qingYiSeMultiple = multipleDic.get(QING_YI_SE_MULTIPLE_ID).getMultiple();
    if (cards.isQingYiSe()) {
      totalMultiple += qingYiSeMultiple;
    }

    /*
     * 四合计算(11:四合) .
     */
    int siHeMultiple = multipleDic.get(SI_HE_MULTIPLE_ID).getMultiple();
    int siHeCount = cards.siHeCount(huType, huPaiType);

    for (int i = 0; i < siHeCount; i++) {
      totalMultiple += siHeMultiple;
    }

    return totalMultiple;
  }

}
